#include"../inc/RssReader.h"
using std::regex;
using std::regex_replace;
using std::endl;

namespace SE
{
    void RssReader::DFS(XMLElement* root)
    {
        while(root)
        {
            if(!strcmp(root->Name(), "item"))
            {
                RssItem it;
                XMLElement* Title = root->FirstChildElement("title");
                XMLElement* Link = root->FirstChildElement("link");
                XMLElement* Description = root->FirstChildElement("description");
                XMLElement* Content = root->FirstChildElement("content:encoded");

                if(Title && Title->GetText())
                {
                    stringstream ss;
                    string s = Title->GetText();
                    s = regex_replace(s, regex("(<)[^>]*(>)"), ""); //<XXX> 这样的形式全部转换为空白
                    ss << regex_replace(s, regex("\\s|\u3000"), ","); // 所有空白全部转换为","
                    it.title = ss.str();
                }
                if(Link && Link->GetText())
                {
                    stringstream ss;
                    string s = Link->GetText();
                    s = regex_replace(s, regex("(<)[^>]*(>)"), ""); //<XXX> 这样的形式全部转换为空白
                    ss << regex_replace(s, regex("\\s|\u3000"), ","); // 所有空白全部转换为","
                    it.link = ss.str();
                }
                if(Description && Description->GetText())
                {
                    stringstream ss;
                    string s = Description->GetText();
                    s = regex_replace(s, regex("(<)[^>]*(>)"), ""); //<XXX> 这样的形式全部转换为空白
                    ss << regex_replace(s, regex("\\s|\u3000"), ","); // 所有空白全部转换为","
                    it.description = ss.str();
                }
                if(Content && Content->GetText())
                {
                    stringstream ss;
                    string s = Content->GetText();
                    s = regex_replace(s, regex("(<)[^>]*(>)"), ""); //<XXX> 这样的形式全部转换为空白
                    ss << regex_replace(s, regex("\\s|\u3000"), ","); // 所有空白全部转换为","
                    it.content = ss.str();
                }
                _rss.push_back(it);
            }
            
            DFS(root->FirstChildElement());
            root = root->NextSiblingElement();
        }
    }

    bool RssReader::ParseRss(const string& file)
    {
        XMLDocument doc;
        if(doc.LoadFile(file.c_str()) != XML_SUCCESS)
        {
            logError("Rss LoadFile");
            return false;
        }

        XMLElement* root = doc.RootElement();
        DFS(root);

        return true;
    }

    void RssReader::Parse(const string& page_dir, const string& offset_dir, vector<string>& page_lib, map<int, pair<int, int>>& offset_lib)
    {
        std::ofstream ofs(page_dir, std::ofstream::out);
        if(!ofs)
        {
            logError("PAGE_DIR OPEN");
            exit(1);
        }

        // recreate the xml page
        for(size_t i = 0; i < _rss.size(); i++)
        {
            stringstream ss;
            ss << "<doc>" << endl;
            ss << "\t <title>" << _rss[i].title << " </titile>" << endl;
            ss << "\t <link>" << _rss[i].link << " </link>" << endl;
            ss << "\t <description> " << _rss[i].description << " </description>" << endl;
            ss << "\t <content> " << _rss[i].content << " </content>" << endl;
            ss << "</doc>" << endl;

            // current to the map elem
            offset_lib[i].first = ofs.tellp();
            ofs << ss.str();
            offset_lib[i].second = ofs.tellp();

            page_lib.push_back(ss.str());
        }
        ofs.close();

        std::ofstream ofs1(offset_dir, std::ofstream::out);
        if(!ofs1)
        {
            logError("OFFSET_DIR OPEN");
            exit(1);
        }
        for(auto & k : offset_lib)
            ofs1 << k.first << " " << k.second.first << " " << k.second.second << endl;

        ofs1.close();
    }
}